<template>
  <div class="movie-detail" v-if="movie">
    <div class="movie-header">
      <div class="movie-poster">
        <img :src="movie.picPaths || '../../assets/images/bg.png'" :alt="movie.title"/>
      </div>
      
      <div class="movie-info">
        <h1>{{ movie.title }}</h1>
        <div class="movie-meta">
          
          <span>商品价格：</span><b class="red">{{ movie.price }}元</b>
          <br>
          <span>发布时间：</span><span>{{ movie.addedTime }}</span>
          <br>
          <span>发布用户：</span><span>{{ movie.authorName }}</span>
        </div>
        
        <div class="movie-rating">
          <span class="stars">★★★★★</span>
        </div>
        
        <div class="movie-actions">
          <button v-if="isLoggedIn" class="btn btn-primary" @click="editMovie">立即购买</button>
          <button v-if="isLoggedIn" class="btn btn-reg" @click="editMovie">添加购物车</button>
        </div>
      </div>
    </div>
    
    <div class="movie-description">
      <h2>商品描述</h2>
      <p>{{ movie.desc }}</p>
    </div>
    
    <CommentSection 
      :movieId="movie.id"
      :movieTitle="movie.title"
      @add-comment="addComment"
    />
  </div>
  
  <div class="loading" v-else-if="isLoading">
    <p>Loading movie details...</p>
  </div>
  
  <div class="error" v-else>
    <p>Movie not found</p>
  </div>
</template>

<script setup>
import { ref, onMounted, computed, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useUserStore } from '@/stores/user';
import apiService from '@/services/api';
import CommentSection from '@/components/comments/CommentSection.vue';

const store = useUserStore();
const isLoggedIn = true;
const movie = ref(null); // 用于存储当前商品详情
const isLoading = ref(true); // 用于控制加载状态
const router = useRouter();
const route = useRoute();
const loading = ref(true);
const error = ref('');
const currentPage = ref(1); // 当前页码
const itemsPerPage = 12; // 每页显示的商品数量
const searchQuery = ref(''); // 搜索查询

const huoqu = async () => {  
  loading.value = true;
  error.value = '';
  try {
    const response = await apiService.getSingle({ 
      id:route.params.id,
    });
    movie.value = response;
  } catch (err) {
    error.value = '获取商品列表失败，请稍后重试';
    console.error('获取商品列表失败:', err);
  } finally {
    loading.value = false;
  }
};

const fetchMovies = async () => {
  loading.value = true;
  error.value = '';
  try {
    const response = await apiService.getAllItems({
      query: searchQuery.value,
      page: currentPage.value,
      limit: itemsPerPage
    });
    movies.value = response.data;
    totalItems.value = response.total;
  } catch (err) {
    error.value = '获取商品列表失败，请稍后重试';
    console.error('获取商品列表失败:', err);
  } finally {
    loading.value = false;
  }
};

const debounceTimeout = ref(null);
const debounceSearch = () => {
  clearTimeout(debounceTimeout.value);
  debounceTimeout.value = setTimeout(() => {
    currentPage.value = 1; // 重置分页状态
    fetchMovies();
  }, 300);
};

onMounted(() => {
  huoqu();
  fetchMovies();
});

watch(
  () => route.params.id,
  (newId) => {
    if (newId) {
      huoqu();
    }
  }
);

watch(
  () => currentPage.value,
  (newPage) => {
    fetchMovies();
  }
);

const editMovie = () => {
  alert("暂无此功能");
};

const addComment = (comment) => {
  console.log('添加评论:', comment);
  // 这里实现添加评论的逻辑
};
</script>

<style scoped>
.movie-detail {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}
.red{
  color:red;
  font-size: 30px;

}
.movie-header {
  display: flex;
  margin-bottom: 30px;
}

.movie-poster {
  flex: 0 0 30%;
  margin-right: 30px;
  width: 400px;
  height: 200px;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
}

.movie-poster img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 8px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

.movie-info {
  flex: 1;
}

.movie-info h1 {
  font-size: 2.5rem;
  margin-bottom: 15px;
  color: #333;
}

.movie-meta {
  margin-bottom: 15px;
}

.movie-year {
  font-weight: bold;
  margin-right: 15px;
  color: #666;
}

.movie-genre {
  background-color: #f0f0f0;
  padding: 5px 10px;
  border-radius: 4px;
  font-size: 0.9rem;
}

.movie-rating {
  margin-bottom: 20px;
}

.stars {
  color: #ffc107;
  margin-right: 10px;
}

.rating {
  font-size: 1.5rem;
  font-weight: bold;
  color: #333;
}

.movie-actions {
  margin-top: 20px;
}

.btn {
  padding: 10px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-weight: bold;
  transition: background-color 0.3s;
}

.btn-primary {
  background: linear-gradient(90deg, rgb(255, 203, 0), rgb(255, 148, 2));
  color: white;
  margin-right: 10px;
}
.btn-reg {
  background: linear-gradient(90deg, rgb(255, 119, 0), rgb(255, 73, 0));
  color: white;
  margin-right: 10px;
}

.btn-danger {
  background-color: #dc3545;
  color: white;
}

.btn-danger:hover {
  background-color: #c82333;
}

.movie-description {
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid #eee;
}

.movie-description h2 {
  font-size: 1.8rem;
  margin-bottom: 15px;
  color: #333;
}

.movie-description p {
  font-size: 1.1rem;
  line-height: 1.6;
  color: #666;
}

.loading {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;
}

.error {
  text-align: center;
  padding: 30px;
  color: #dc3545;
  font-size: 1.2rem;
}

/* 媒体查询：屏幕宽度小于768px时 */
@media (max-width: 768px) {
  .movie-header {
    flex-direction: column;
    align-items: center;
  }

  .movie-poster {
    margin-right: 0;
    margin-bottom: 20px;
    width: 100%;
    height: 300px; /* 设置最大高度为200px */
  }

  .movie-poster img {
    width: 100%;
    height: 300px; /* 保持图片高度不超过200px */
    object-fit: cover; /* 确保图片覆盖整个容器 */
  }

  .movie-info {
    text-align: center;
  }

  .movie-info h1 {
    font-size: 2rem;
  }

  .movie-meta {
    margin-bottom: 10px;
  }

  .movie-rating {
    margin-bottom: 15px;
  }

  .movie-actions {
    margin-top: 15px;
  }

  .btn {
    margin: 5px;
  }
}
</style>